package com.czk.utlls;

import com.alibaba.fastjson.JSONObject;
import com.czk.pojo.Ticket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/20 16:15
 */
@Component
public class QrCodeUtil {
    @Autowired
    private TokenUtil tokenUtil;
    String tempQrCodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";

    public Ticket getTicket() {
        // 获取token
        tempQrCodeUrl = tempQrCodeUrl.replace("TOKEN", tokenUtil.getToken().getAccessToken());
        //
        String context = "{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 2}}}";
        // 获取ticket
        String result = WeChatUtil.post(tempQrCodeUrl, context);
        // 转为
        Ticket ticket = JSONObject.parseObject(result, Ticket.class);
        // Ticket(ticket=gQHd8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyclItMUp0dktkSUQxMDAwMHcwM2EAAgSoZh9hAwQAAAAA,
        // expire_seconds=null, url=http://weixin.qq.com/q/02rR-1JtvKdID10000w03a)
        System.out.println(ticket);
        return ticket;
    }
}
